Create map widget with leaflet()
library(leaflet)
library(sf)
Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
beetle_url <- "https://raw.githubusercontent.com/dyerlab/ENVS-Lectures/master/data/Araptus_Disperal_Bias.csv"
beetle <- read.csv(beetle_url)
beetle <- beetle %>%
st_as_sf( coords=c("Longitude","Latitude"), crs=4326 )
Add a basemap with addTiles(). By default OpenStreetMap tiles are used.
leaflet() %>%
setView(lng = -77.5, lat = 37.5, zoom = 10) %>% #not necessary to set your view
addTiles()
#blank map because we haven't added any layers
Use addProviderTiles() function to use a different basemap.
See here for all options
leaflet() %>%
setView(lng = -77.5, lat = 37.5, zoom = 10) %>% #not necessary to set your view
addProviderTiles(providers$CartoDB.Positron)
sp objectssf objectsmaps package map() objectsData can be passed through the leaflet() function or through the map layers.
#These make the same map
leaflet(beetle) %>%
addTiles() %>%
addCircles()
leaflet() %>%
addTiles() %>%
addCircles(data = beetle)
Points can be plotted using addMarkers() or addCircles() - Markers stay the same size regardless of zoom level - Circles scale with the map
leaflet(beetle) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addMarkers()
Custom Markers - Custom markers can be made using a url/image file or from libraries
bug <- icons(
iconUrl = "https://www.pngfind.com/pngs/m/14-144860_beetle-bug-png-transparent-image-bug-png-png.png",
iconWidth = 20,
iconHeight = 20
)
leaflet(beetle) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addMarkers(icon = bug)
fa_bug <- makeAwesomeIcon(icon = "bug", library = "fa",
markerColor = "cadetblue", iconColor = "beige")
leaflet(beetle) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addAwesomeMarkers(icon = fa_bug)
Popups can be added as a stand-alone feature using addPopups(), or add to appear when a shape is clicked
#stand-alone
leaflet(beetle) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
setView(lng = -111, lat = 25, zoom = 7) %>%
addPopups(lng = -111, lat = 25, paste0("beetles live here"))
#as a part of a marker
leaflet(beetle) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addAwesomeMarkers(icon = fa_bug, popup = paste0("Site:", beetle$Site))
Labels
Use label = to add a label displayed on a mouse over
leaflet(beetle) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addAwesomeMarkers(icon = fa_bug, label = paste0("Site:", beetle$Site))
addCircles()
leaflet(beetle) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircles()
Change radius:
leaflet(beetle) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircles(radius = ~Males *500, stroke = FALSE, fillOpacity = .5)
Change color:
pal <- colorNumeric(
palette = "RdBu",
domain = beetle$MFRatio
)
leaflet(beetle) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircles(color = ~rev(pal(MFRatio)), fillOpacity = .7, radius = 15000, stroke = FALSE)
Highlight
leaflet(beetle) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircles(fillColor = ~rev(pal(MFRatio)), fillOpacity = .7, radius = 15000,
weight = 1, color = "grey",
highlightOptions = highlightOptions(color = "green", weight = 2,
bringToFront = TRUE))
Polygons
library(tidyverse)
-- Attaching packages --------------------------------------- tidyverse 1.3.0 --
v ggplot2 3.3.3 v purrr 0.3.4
v tibble 3.0.5 v dplyr 1.0.4
v tidyr 1.1.2 v stringr 1.4.0
v readr 1.4.0 v forcats 0.5.1
Warning: package 'dplyr' was built under R version 4.0.4
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
library(tigris)
To enable
caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
options(tigris_use_cache = TRUE)
tracts <- tracts("VA", "Richmond city") %>%
st_transform(4326)
leaflet(tracts) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addPolygons()
Add scale bar with addScaleBar()
pal <- colorNumeric(palette = "Blues",
domain = tracts$ALAND/2.59e+6)
leaflet(tracts) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addPolygons(fillColor = ~pal(ALAND/2.59e+6), fillOpacity = 1, smoothFactor = .2,
color = "darkgrey", weight = 1,
highlightOptions = highlightOptions(color = "white", weight = 2, opacity = 1,
bringToFront = TRUE),
label = paste(round(tracts$ALAND/2.59e+6, digits = 2), "sq. mi.")
) %>%
addLegend(pal = pal, values = ~ALAND/2.59e+6, title = "Area (sq. miles)") %>%
addScaleBar(position = "bottomright")
Leaflet Extras
addDrawToolbar()
library(leaflet.extras)
leaflet(tracts) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addPolygons(fillColor = ~pal(ALAND/2.59e+6), fillOpacity = 1, smoothFactor = .2,
color = "darkgrey", weight = 1,
highlightOptions = highlightOptions(color = "white", weight = 2, opacity = 1,
bringToFront = TRUE),
label = paste(round(tracts$ALAND/2.59e+6, digits = 2), "sq. mi.")
) %>%
addLegend(pal = pal, values = ~ALAND/2.59e+6, title = "Area (sq. miles)") %>%
addScaleBar(position = "bottomright") %>%
addDrawToolbar()